<?php


namespace App\Api\Models\user;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CmcPointLog extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $guarded = [];
    protected $table ='cmc_point_log';

    /**
     * 添加和修改
     * @param $data
     * @param array $where
     * @return \Illuminate\Database\Eloquent\Builder|Model|int
     */
    public static function saveData($data,$where = [])
    {
        if (!empty($where) && is_array($where)){
            return self::query()->where($where)->update($data);
        }
        return self::query()->create($data);
    }

    /**
     * 累计获得积分 和累计消费
     * @param $memberId
     * @param $type
     * @return mixed
     */
    public static function Point($memberId,$type = 1)
    {
        return self::where('customer_id',$memberId)->where('status',$type)->sum('change_point');
    }

    /**
     * 当日获得
     * @param $memberId
     * @return int|mixed
     */
    public static function dayPoint($memberId)
    {
        $time = date('Y-m-d',time());
        return self::query()->where('customer_id',$memberId)
            ->where('status',1)
            ->where('create_time','like', "$time%")
            ->sum('change_point');
    }


}
